//index.js
const app = getApp()

Page({
  data: {
    invoice: {},
    index: 0,
    billTables: []
  },

  onLoad: function (options) {
    if (!wx.cloud) {
      wx.redirectTo({
        url: '../chooseLib/chooseLib',
      })
      return
    }
    
    if (app.globalData.billTables.length == 0 || Object.keys(app.globalData.billTables).length == 0) {
      this.queryData()
    } else {
      this.setData({
        billTables: app.globalData.billTables
      })
    }

    console.log("invoiceBilling onLoad options => ", options)
    console.log("invoiceBilling onLoad app.globalData.invoice => ", app.globalData.invoice)

    this.setData({
      invoice: app.globalData.invoice
    })
    
    // if (options.invoiceId != null) {
    //   wx.cloud.callFunction({
    //     name: 'invoice_get_by_id',
    //     data: {
    //       invoice_id: options.invoiceId
    //     },
    //     success: res => {
    //       console.log("billing result ==> ", res)
    //       wx.hideLoading()
    //       if (res.result.code == 1) {
    //         this.setData({
    //           invoice: res.result.data
    //         })
    //       }
    //     },
    //     fail: err => {
    //       wx.hideLoading()
    //       wx.showToast({
    //         icon: 'none',
    //         title: '发票抬头加载失败',
    //       })
    //       console.error('[云函数] [invoice_get_by_id] 调用失败：', err)
    //     }
    //   })
    // }
    
  },

  //下拉刷新
  onPullDownRefresh: function () {
    console.log('invoiceBilling onPullDownRefresh')
    //重新加载
    this.queryData()
  },

  billSelected: function(e) {
    console.log('pick value: ', e.detail.value)
    this.setData({
      index: e.detail.value
    })
  },

  billSubmit: function (e) {
   
    console.log(this.data.invoice);
    console.log(this.data.index);
    console.log(this.data.billTables);
    
    wx.showLoading({
      title: '提交中',
    })
    setTimeout(function () {
      wx.hideLoading()
    }, 2000)

    var bill = {
      billTable: this.data.billTables[this.data.index],
      createDate: Date.now(),
      invoice: this.data.invoice,
      //消费金额，服务端添加
      fee: -1,
      //0-处理中，1-已开票，2-已废弃
      status: 0
    }

    console.log("invoiceBilling submit => ", bill)

      wx.cloud.callFunction({
        name: 'bill_add',
        data: {
          bill: bill
        },
        success: res => {
          console.log("invoiceBilling bill_add => ", res)
          wx.hideLoading()
          if (res.result.code == 1) {
            wx.navigateBack({
              success: function() {
                wx.showToast({
                  title: '发票申请已提交',
                })
              }
            })
          }
        },
        fail: err => {
          wx.hideLoading()
          wx.showToast({
            icon: 'warn',
            title: '提交失败请重试',
          })
          console.error('[云函数] [bill_add] 调用失败：', err)
        }
      })
    },

    queryData: function() {
      wx.showLoading({
        title: '加载中',
      })
      setTimeout(function () {
        wx.hideLoading()
      }, 2000)

      wx.cloud.callFunction({
        name: 'bill_table_query',
        data: {
        },
        success: res => {
          console.log("invoiceBilling bill_table_query => ", res)
          app.globalData.billTables = res.result.data
          this.setData({
            billTables: res.result.data
          })
        },
        fail: err => {
          wx.showToast({
            icon: 'none',
            title: '信息加载失败',
          })
          console.error('[云函数] [bill_table_query] 调用失败：', err)
        }
      })
    },

})
